#include <QtWidgets>

#include "treeitem.h"
#include "treemodel.h"

TreeModel::TreeModel(const QStringList &headers, QObject *parent)
    : QAbstractItemModel(parent)
{
//    QVector<QVariant> rootData;
//    foreach (QString header, headers)
//        rootData << header;

//    rootItem = new TreeItem(rootData);

    QVector<QVariant> rootData;
    rootData << "Title" << "summary";
    rootItem = new TreeItem(rootData);

    rootItem->insertChildren(rootItem->childCount(), 1, rootItem->columnCount());

    QVector<QVariant> gettingStartedData;
    gettingStartedData << "Getting Started" << "How to familiarize yourself with Qt Designer";
    rootItem->child(rootItem->childCount()-1)->setData(0, gettingStartedData[0]);
    rootItem->child(rootItem->childCount()-1)->setData(1, gettingStartedData[1]);

    TreeItem *gettingStartedItem = rootItem->child(rootItem->childCount()-1);
    gettingStartedItem->insertChildren(gettingStartedItem->childCount(), 1, rootItem->columnCount());

    QVector<QVariant> launchingDesignerData;
    launchingDesignerData << "Launching Designer" << "Running the Qt Designer application";
    gettingStartedItem->child(gettingStartedItem->childCount()-1)->setData(0, launchingDesignerData[0]);
    gettingStartedItem->child(gettingStartedItem->childCount()-1)->setData(1, launchingDesignerData[1]);

    gettingStartedItem->insertChildren(gettingStartedItem->childCount(), 1, rootItem->columnCount());
    QVector<QVariant> userInterfaceData;
    userInterfaceData << "The User Interface" << "How to interact with Qt Designer";
    gettingStartedItem->child(gettingStartedItem->childCount()-1)->setData(0, userInterfaceData[0]);
    gettingStartedItem->child(gettingStartedItem->childCount()-1)->setData(1, userInterfaceData[1]);

    QVector<QVariant> designingComponentData;
    designingComponentData << "Designing a Component" << "Creating a GUI for your application";
    rootItem->insertChildren(rootItem->childCount(), 1, rootItem->columnCount());
    rootItem->child(rootItem->childCount()-1)->setData(0, designingComponentData[0]);
    rootItem->child(rootItem->childCount()-1)->setData(1, designingComponentData[1]);

    TreeItem *designingComponentItem = rootItem->child(rootItem->childCount()-1);

    QVector<QVariant> creatingDialogData;
    creatingDialogData << "Creating a Dialog" << "How to create a dialog";

    QVector<QVariant> composingDialogData;
    composingDialogData << "Composing the Dialog" << "Putting widgets into the dialog example";

    designingComponentItem->insertChildren(designingComponentItem->childCount(), 2, rootItem->columnCount());
    designingComponentItem->child(0)->setData(0, creatingDialogData[0]);
    designingComponentItem->child(0)->setData(1, creatingDialogData[1]);

    designingComponentItem->child(1)->setData(0, composingDialogData[0]);
    designingComponentItem->child(1)->setData(1, composingDialogData[1]);
}

TreeModel::~TreeModel()
{
    delete rootItem;
}

int TreeModel::columnCount(const QModelIndex & /* parent */) const
{
    return rootItem->columnCount();
}

QVariant TreeModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role != Qt::DisplayRole && role != Qt::EditRole)
        return QVariant();

    TreeItem *item = getItem(index);

    return item->data(index.column());
}

Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;
    qDebug("233233");
    return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
    //return QAbstractItemModel::flags(index);
}

TreeItem *TreeModel::getItem(const QModelIndex &index) const
{
    if (index.isValid()) {
        TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
        if (item)
            return item;
    }
    return rootItem;
}


QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
                               int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
        return rootItem->data(section);

    return QVariant();
}


QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
    if (parent.isValid() && parent.column() != 0)
        return QModelIndex();

    TreeItem *parentItem = getItem(parent);

    TreeItem *childItem = parentItem->child(row);
    if (childItem)
        return createIndex(row, column, childItem);
    else
        return QModelIndex();
}


bool TreeModel::insertColumns(int position, int columns, const QModelIndex &parent)
{
    bool success;

    beginInsertColumns(parent, position, position + columns - 1);
    success = rootItem->insertColumns(position, columns);
    endInsertColumns();

    return success;
}

bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
{
    TreeItem *parentItem = getItem(parent);
    bool success;

    beginInsertRows(parent, position, position + rows - 1);
    success = parentItem->insertChildren(position, rows, rootItem->columnCount());
    endInsertRows();

    return success;
}


QModelIndex TreeModel::parent(const QModelIndex &index) const
{
    if (!index.isValid())
        return QModelIndex();

    TreeItem *childItem = getItem(index);
    TreeItem *parentItem = childItem->parent();

    if (parentItem == rootItem)
        return QModelIndex();

    return createIndex(parentItem->childNumber(), 0, parentItem);
}

bool TreeModel::removeColumns(int position, int columns, const QModelIndex &parent)
{
    bool success;

    beginRemoveColumns(parent, position, position + columns - 1);
    success = rootItem->removeColumns(position, columns);
    endRemoveColumns();

    if (rootItem->columnCount() == 0)
        removeRows(0, rowCount());

    return success;
}

bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
{
    TreeItem *parentItem = getItem(parent);
    bool success = true;

    beginRemoveRows(parent, position, position + rows - 1);
    success = parentItem->removeChildren(position, rows);
    endRemoveRows();

    return success;
}

int TreeModel::rowCount(const QModelIndex &parent) const
{
    TreeItem *parentItem = getItem(parent);

    return parentItem->childCount();
}

bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (role != Qt::EditRole)
        return false;

    TreeItem *item = getItem(index);
    bool result = item->setData(index.column(), value);

    if (result)
        emit dataChanged(index, index);

    return result;
}

bool TreeModel::setHeaderData(int section, Qt::Orientation orientation,
                              const QVariant &value, int role)
{
    if (role != Qt::EditRole || orientation != Qt::Horizontal)
        return false;

    bool result = rootItem->setData(section, value);

    if (result)
        emit headerDataChanged(orientation, section, section);

    return result;
}
